$.extend({ outer: {} });
$.extend($.outer, {
  init: function () {
    this.domainUrl = window.location.protocol + "//" + window.location.host;

    //正式环境处理
    this.getData(); //获取接口数据

    //测试环境处理
    // this.testOcjData();
    // this.testGetData(); //测试环境
  },
  testOcjData() {
    console.log(experimentData);

    $.loading.close();
    this.handleOpenedExperiment();
    $.tabComp.init(); //tab组件初始化
    $.propComp.init(); //属性面板抽屉组件初始化
    this.scrollToViewCenter(); //滚动到画布中间
  },
  testGetData() {
    console.log(experimentData1);
    var scriptVal = experimentData1.initScript;
    xmlDoc = $.parseXML(scriptVal);
    if (xmlDoc) {
      var tabId = $(xmlDoc).find("tabId").text();
      var tabIdArray = tabId.split("id");
      window.experimentData = {}; //实验数据
      tabIdArray.forEach((item) => {
        if (item) {
          var exper = `exper${item}`;
          var experString = decodeURIComponent(escape(window.atob($(xmlDoc).find(exper).text())));
          experObject = JSON.parse(experString);
          experimentData[item] = experObject;
        }
      });
      console.log(tabIdArray);
      console.log(experimentData);

      $.loading.close();
      this.handleOpenedExperiment();
      $.tabComp.init(); //tab组件初始化
      $.propComp.init(); //属性面板抽屉组件初始化
      this.scrollToViewCenter(); //滚动到画布中间
    }
  },
  getData: function () {
    $.loading.open();
    var that = this;
    let arrangementId = this.getUrlParam("arrangementId") || "ff80808186543f2b0186543fab670013";
    $.ajax({
      // timeout: 20000,
      type: "GET",
      // dataType: "json",
      // contentType: "application/json;charset=UTF-8",
      // url: `${this.domainUrl}/lab/han、dle/!phoneInitScript?arrangementId=${this.getUrlParam("arrangementId")}`,
      url: `http://test184.owvlab.com/openlab/lab/handle/!phoneInitScript?arrangementId=${arrangementId}`,
      success: function (data) {
        $.loading.close();
        console.log(data);
        if (data.code !== 200) {
          layer.msg(`请联系管理员，code:${data.code}`);
          return;
        }
        let scriptVal = data.initScript;
        let xmlDoc = $.parseXML(scriptVal);
        if (xmlDoc) {
          var tabId = $(xmlDoc).find("tabId").text();
          var tabIdArray = tabId.split("id");
          window.experimentData = {}; //实验数据
          tabIdArray.forEach((item) => {
            if (item) {
              var exper = `exper${item}`;
              var experString = decodeURIComponent(escape(window.atob($(xmlDoc).find(exper).text())));
              experObject = JSON.parse(experString);
              experimentData[item] = experObject;
            }
          });

          that.handleOpenedExperiment();
          $.tabComp.init(); //tab组件初始化
          $.propComp.init(); //属性面板抽屉组件初始化
          that.scrollToViewCenter(); //滚动到画布中间
        }
      },
      error: function (jqXHR, textStatus, errorThrown) {
        $.loading.close();
        console.log(jqXHR.responseText);
      },
    });
  },
  handleOpenedExperiment: function () {
    //删除原来的实验内容
    var _this = this;

    window.data = [];
    window.usedPartArray = [];

    //首页tab界面 实验回显
    var context = window.app.getContext();
    var expActiveJson = experimentData.firstPage.json || experimentData.firstPage;
    activeEditor = context.getActiveEditor();
    expActiveJson && activeEditor.doRestore(expActiveJson);

    //恢复隐藏图片库
    window.hideImgArr = {};
    var hideImgId = experimentData.firstPage.hideImgId;
    var keys = Object.keys(hideImgId);
    $.each(keys, function (index, key) {
      var idArr = [];
      if (key == "#FirstPage") {
        window.app = appObj["firstPage"];
      } else {
        window.app = appObj[key.substring(11)];
      }
      $.each(hideImgId[key], function (i, id) {
        var rootModel = window.app.context.activeEditor.context.getRootModel();
        var model = _this.getModelById(rootModel, id);
        var deleteInfo = rootModel.removeChildModel(model);
        idArr.push(deleteInfo);
      });
      hideImgArr[key] = idArr;
    });
    // this.setDefaultZoom();
  },
  setDefaultZoom() {
    let context = window.app.getContext();
    let activeEditor = context.getActiveEditor();
    let canvas = activeEditor.canvas;
    canvas.setZoom(window.zoom, true);
  },

  // 兼容写法，获取滚动条的高度
  getWindowScrollTop: function () {
    var scroll_top = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
      scroll_top = document.documentElement.scrollTop;
    } else if (document.body) {
      scroll_top = document.body.scrollTop;
    }
    return scroll_top;
  },
  /**
   * 滚动到canvas视图中心
   */
  scrollToViewCenter: function () {
    window.scrollTo({
      top: canvasHeight / 2,
      left: canvasWidth / 2,
      behavior: "smooth",
    });
  },
  // scrollToViewCenter: function () {
  //   console.log("[ el.getBoundingClientRect() ] >", this.canvasDom.getBoundingClientRect());
  //   const { top, height } = this.canvasDom.getBoundingClientRect();
  //   // 元素的中心高度
  //   const elCenter = top + height / 2;
  //   // 窗口的中心高度
  //   const innerHeightHalf = window.innerHeight / 2;
  //   const innerWidthHalf = window.innerWidth / 2;
  //   window.scrollTo({
  //     top: this.getWindowScrollTop() - (innerHeightHalf - elCenter),
  //     left: this.getWindowScrollTop() - (innerWidthHalf - elCenter),
  //     behavior: "smooth",
  //   });
  // },
  scrollToEelCenter: function (x, y) {
    const { top, height } = this.canvasDom.getBoundingClientRect();
    // 元素的中心高度
    const elCenter = top + height / 2;
    // 窗口的中心高度
    const innerHeightHalf = window.innerHeight / 2;
    const innerWidthHalf = window.innerWidth / 2;
    window.scrollTo({
      top: this.getWindowScrollTop() - (innerHeightHalf - y / 2),
      left: this.getWindowScrollTop() - (innerWidthHalf - x / 2),
      behavior: "smooth",
    });
  },
  //获取参数
  getUrlParam: function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
  },
});
